package mil.af.amc.adpetracker.web.actions.EquipmentItem;

import mil.af.amc.adpetracker.web.actions.AbstractAction;
import mil.af.amc.adpetracker.service.EquipmentItemService;
import mil.af.amc.adpetracker.service.ServiceException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Please Fill in this Javadoc
 *
 * @author TSgt Jason Ferguson
 */

public class TransferItemAction extends AbstractAction {

    private static final Log log = LogFactory.getLog(TransferItemAction.class);

    private EquipmentItemService equipmentItemService;

    public void setEquipmentItemService(EquipmentItemService equipmentItemService) {
        this.equipmentItemService = equipmentItemService;
    }

    @Override
    public ActionForward execute(ActionMapping actionMapping,
                                 ActionForm actionForm,
                                 HttpServletRequest httpServletRequest,
                                 HttpServletResponse httpServletResponse) throws Exception {

        DynaActionForm form = (DynaActionForm) actionForm;

        try {

            equipmentItemService.transferItems((Integer[]) form.get("pkgItemId"), (Integer) form.get("adpeAccountId"));
        } catch (ServiceException e) {

            log.debug(e.getMessage(), e);
            ActionMessage msg = new ActionMessage(e.getKey(), e.getMessage());
            saveError(httpServletRequest, msg);

            return actionMapping.getInputForward();
        }

        return actionMapping.findForward("success");

    }
}
